#include <soar.h>

int main(int argc, char *argv[])
{
    int res;
    int i;
    char *host_addr;
    unsigned int connect_flag;
    unsigned int rwflag;
    SKT skt;
    char *msg_in;
    int msg_in_size;
    int timeout;

    if(argc != 3)
    {
        printf("client: parameter error\n");
        return(-1);
    }
    connect_flag=0;
    if(strcasecmp(argv[1], "LOCAL") == 0)
    {
        host_addr="samplefunc_skt";
        connect_flag|=FLAG_LOCAL;
    }
    else
    {
        host_addr="localhost:20000";
    }
    if(strcasecmp(argv[2], "RAW") == 0)
        rwflag=SKT_W_SIZE;
    else if(strcasecmp(argv[2], "SIZE") == 0)
        rwflag=SKT_W_SIZE;
    else if(strcasecmp(argv[2], "WRAP") == 0)
        rwflag=SKT_W_WRAP;
    else
        rwflag=SKT_W_SIZE;
    
    timeout=5*1000;
    if((res=skt_connect(host_addr, connect_flag, &timeout, &skt)))
    {
        printf("client: connect fail, res:%d\n", res);
        return(-1);
    }
    printf("client: self: %s %d\nclient: peer: %s %d\n",
            skt->sock_info.self_ip, skt->sock_info.self_port,
            skt->sock_info.peer_ip, skt->sock_info.peer_port);

	ALLOC_MULTI_DOERR(msg_in, char, SOCK_MSG_SIZE,
           printf("client: alloc fail\n"); return(-1));

    switch(rwflag)
    {
        case SKT_W_SIZE:
            msg_in_size=KB(128);
            memset(msg_in, '0', msg_in_size);
            timeout=600*1000;
            if((res=skt_write(skt,
                            (unsigned char *)msg_in, &msg_in_size,
                            rwflag, &timeout)) < 0)
            {
                printf("client: write fail, res:%d\n", res);
                return(-1);
            }
            printf("write: %d bytes\n", res);
            break;

        case SKT_W_WRAP:
            for(i=0; i<3; i++)
            {
                msg_in_size=200;
                memset(msg_in, '0', msg_in_size);
                msg_in[msg_in_size]=ZERO;
                timeout=600*1000;
                if((res=skt_write(skt,
                                (unsigned char *)msg_in, &msg_in_size,
                                rwflag, &timeout)) < 0)
                {
                    printf("client: write fail, res:%d\n", res);
                    return(-1);
                }
            }
            DEEP_SLEEP;
            break;
    }
    sleep(10);
    skt_close(skt);

    return(0);
}
